const webpack = require('webpack');
const path = require('path');
const getEnvConfig = require('./config');
const paths = require('react-scripts/config/paths');
const {
  override,
  addWebpackPlugin,
  addWebpackAlias,
} = require('customize-cra');
const rewirePostcss = require('react-app-rewire-postcss');
const px2rem = require('postcss-px2rem');

paths.appBuild = path.join(
  path.dirname(paths.appBuild),
  '/build/' + process.env.CONFIG_ENV
);

module.exports = override(
  addWebpackPlugin(new webpack.DefinePlugin(getEnvConfig(process.env))),
  addWebpackAlias({
    '@components': path.resolve(__dirname, './src/component'),
    '@utils': path.resolve(__dirname, './src/utils'),
    '@service': path.resolve(__dirname, './src/service'),
    '@global': path.resolve(__dirname, './src/global'),
    '@pages': path.resolve(__dirname, './src/pages'),
  }),
  (config, env) => {
    rewirePostcss(config, {
      plugins: () => [px2rem({ remUnit: 75 })],
    });
    if (process.env.NODE_ENV === 'production') config.devtool = false;
    return config;
  }
);
